package user.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;

public class ExcelView extends AbstractView {
	public ExcelView()      {   
		super();    
		setContentType( "application/octet-stream"); 
		}   
 	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest arg1, HttpServletResponse response) throws Exception {
 		//System.out.println("111");
		String excelPath = (String)model.get("excelPath");
		File file=new File(excelPath);
		String filename=file.getName();
		System.out.println("DOWN:"+filename);
		//System.out.println((int)file.length());
		byte[] bytestream=new byte[(int)file.length()];
		FileInputStream fis=new FileInputStream(file);
		int i=0;
		int j=0;
		while((i=fis.read())!=-1){
		  bytestream[j]=(byte)i;
		  j++;
		}
		fis.close();
		//System.out.println(jobj);
		response.setContentType("application/octet-stream"); 
		response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
		response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache"); 
        response.setContentLength((int) file.length()); 
		OutputStream out=response.getOutputStream();
		//PrintWriter out = response.getWriter();   
		out.write(bytestream);		
		out.close();
		} 
	}
